/**
 * PageMarket
 * PushService.java
 * com.android.pagemarket.ui.service
 * 
 */
package com.android.pagemarket.ui.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.android.pagemarket.MyApplication;
import com.android.pagemarket.business.logic.ContentManager;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-12-12
 * @see : 推送服务
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class PushService extends Service
{   
    public static final String NET_ENABLE_ACTION = "com.android.net_enable_action";
    private boolean isFirst = true;
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        if(MyApplication.getContext().isPushOn())
        {
            ContentManager.getInstance().startPush();
        }else
        {
            stopSelf();
        }
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        if(intent != null )
        {
            String action = intent.getAction();
            if(action != null && action.equals(NET_ENABLE_ACTION))
            {
                // 网络可用，第一次全推送
                if(!isFirst)
                {
                    Log.d("service", NET_ENABLE_ACTION);
                    ContentManager.getInstance().startPushOnce();
                }else
                {
                    isFirst = false;
                }
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }
        
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    public static void startService()
    {
        MyApplication.getContext().startService(new Intent(MyApplication.getContext(), PushService.class));
    }
    
    public static void stotService()
    {
        MyApplication.getContext().stopService(new Intent(MyApplication.getContext(), PushService.class));
    }
}